<template>
  <div class="borrow-page">
    <el-card>
      <h2>借阅办理/续借/归还</h2>
      <el-form :inline="true" @submit.prevent>
        <el-form-item label="借书人ID">
          <el-input v-model="search.borrowerId" placeholder="输入借书人ID" />
        </el-form-item>
        <el-form-item>
          <el-button @click="getList">查询记录</el-button>
          <el-button type="primary" @click="showBorrow = true">办理借书</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="list" style="width: 100%; margin-top: 20px;">
        <el-table-column prop="recordId" label="记录ID" width="70" />
        <el-table-column prop="borrowerId" label="借书人">
          <template #default="scope">
            {{ borrowerMap[scope.row.borrowerId] || scope.row.borrowerId }}
          </template>
        </el-table-column>
        <el-table-column prop="bookId" label="图书" min-width="120" :show-overflow-tooltip="true">
          <template #default="scope">
            {{ bookMap[scope.row.bookId] || scope.row.bookId }}
          </template>
        </el-table-column>
        <el-table-column prop="borrowTime" label="借阅时间">
          <template #default="scope">
            {{ scope.row.borrowTime ? scope.row.borrowTime.slice(0, 10) : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="planReturnTime" label="计划归还">
          <template #default="scope">
            {{ scope.row.planReturnTime ? scope.row.planReturnTime.slice(0, 10) : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="actualReturnTime" label="实际归还">
          <template #default="scope">
            {{ scope.row.actualReturnTime ? scope.row.actualReturnTime.slice(0, 10) : '' }}
          </template>
        </el-table-column>
        <el-table-column prop="renewCount" label="续借次数" />
        <el-table-column prop="status" label="状态">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : (scope.row.status === 2 ? 'info' : 'danger')">
              {{ scope.row.status === 1 ? '借阅中' : (scope.row.status === 2 ? '已归还' : '逾期未还') }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300">
          <template #default="scope">
            <el-button size="small" @click="renewRow(scope.row)" :disabled="scope.row.status !== 1">续借</el-button>
            <el-button size="small" type="primary" @click="returnRow(scope.row)" :disabled="scope.row.status !== 1">归还</el-button>
            <el-button size="small" type="danger" @click="delRow(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        v-model:current-page="page"
        :page-size="size"
        :total="total"
        @current-change="getList"
        layout="prev, pager, next"
        style="margin-top: 20px; text-align: right;"
      />
      <el-dialog v-model="showBorrow" title="办理借书">
        <el-form :model="form">
          <el-form-item label="借书人ID"><el-input v-model="form.borrowerId" /></el-form-item>
          <el-form-item label="图书ID"><el-input v-model="form.bookId" /></el-form-item>
          <el-form-item label="计划归还时间"><el-date-picker v-model="form.planReturnTime" type="datetime" /></el-form-item>
          <el-form-item label="操作员ID"><el-input v-model="form.operatorId" /></el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="showBorrow = false">取消</el-button>
          <el-button type="primary" @click="borrowRow">确定</el-button>
        </template>
      </el-dialog>
    </el-card>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const list = ref([])
const page = ref(1)
const size = ref(10)
const total = ref(0)
const search = reactive({ borrowerId: '' })
const showBorrow = ref(false)
const form = reactive({})

// 新增：借书人ID到姓名的映射
const borrowerMap = reactive({})

// 新增：图书ID到名称的映射
const bookMap = reactive({})

const getBorrowers = async () => {
  const res = await axios.get('/api/borrower/list', { params: { page: 1, size: 1000 } })
  res.data.records.forEach(b => {
    borrowerMap[b.borrowerId] = b.name
  })
}

const getBooks = async () => {
  const res = await axios.get('/api/book/list', { params: { page: 1, size: 1000 } })
  res.data.records.forEach(b => {
    bookMap[b.bookId] = b.title
  })
}

const getList = async () => {
  await getBorrowers()
  await getBooks()
  const res = await axios.get('/api/borrow/records', { params: { page: page.value, size: size.value, borrowerId: search.borrowerId } })
  list.value = res.data.records
  total.value = res.data.total
}
onMounted(getList)

const borrowRow = async () => {
  await axios.post('/api/borrow/borrow', form)
  ElMessage.success('借书成功')
  showBorrow.value = false
  getList()
}
const renewRow = async (row) => {
  await axios.post('/api/borrow/renew', { recordId: row.recordId })
  ElMessage.success('续借成功')
  getList()
}
const returnRow = async (row) => {
  await axios.post('/api/borrow/return', { recordId: row.recordId })
  ElMessage.success('归还成功')
  getList()
}
const delRow = async (row) => {
  try {
    await axios.post('/api/borrow/delete', { recordId: row.recordId })
    ElMessage.success('删除成功')
    getList()
  } catch (e) {
    ElMessage.error(e.response?.data || '删除失败')
  }
}
</script>

<style scoped>
.borrow-page {
  width: 100vw;
  min-height: 100vh;
  background: #f5f6fa;
  padding: 0;
  margin: 0;
}
.el-card {
  width: 100%;
  box-sizing: border-box;
  margin: 0;
  padding: 0 20px;
  background: #fff;
}
.el-table {
  width: 100%;
}
</style> 